package cn.rkylin.oms.base.refundReasonType.utils.apprhandler.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageInfo;

import cn.rkylin.oms.aftersale.resend.service.IAfterSaleResendService;
import cn.rkylin.oms.aftersale.resend.vo.ResendVO;
import cn.rkylin.oms.aftersale.sysrefund.vo.SysRefundVO;
import cn.rkylin.oms.base.refundReasonType.utils.apprhandler.ApprHandler;
import cn.rkylin.oms.common.consts.ResendCancelStatus;
import cn.rkylin.oms.common.consts.ResendStatus;
import cn.rkylin.oms.warehouse.shiporder.service.IShiporderService;
import cn.rkylin.oms.warehouse.shiporder.vo.ShiporderVO;

/**
 * 类名:HasProcessingRecipt <br/>
 * 作用: 退款单审核校验类，校验有进行中的单据（未系统发货前出库单，未完成的补发单）. <br/>
 * 创建时间: 2017年11月3日 下午2:37:55 <br/>
 * 
 * @author zhanghao
 * @version v1.0
 */
public class HasProcessingReciptHandler extends ApprHandler {
	private String ruleContent = "有进行中的单据（未系统发货前出库单、未完成的补发单）；";

	@Autowired
	IShiporderService shipOrderService;
	/**
	 * afterSaleResendService:售后事件服务层.
	 */
	@Autowired
	public IAfterSaleResendService afterSaleResendService;

	/**
	 * @see cn.rkylin.oms.base.refundReasonType.utils.apprhandler.ApprHandler#getRuleContent()
	 */
	@Override
	protected String getRuleContent() {
		return ruleContent;
	}

	/**
	 * @see cn.rkylin.oms.base.refundReasonType.utils.apprhandler.ApprHandler#isValid(cn.rkylin.oms.aftersale.sysrefund.vo.SysRefundVO)
	 */
	@Override
	protected boolean isValid(SysRefundVO sysRefundVO) throws Exception {
		boolean returnValue = true;
		/*
		 * 如果该原因的退款单关联订单所有关联单据有进行中的单据:<br/> （包括未取消且未系统发货前出库单、或未完成且未废弃的补发单），<br/>
		 * 审核该原因的退款单失败，要给出失败原因；<br/> 如果不存在上述进行中单据，该条规则校验通过。<br/>
		 */
		// 1. 校验是否有不符合的出库单
		ShiporderVO shiporderVO = new ShiporderVO();
		shiporderVO.setSourceId(sysRefundVO.getOrderId());
		shiporderVO.setPrjId(sysRefundVO.getPrjId());
		shiporderVO.setSourceType("or");
		shiporderVO.setShiporderStatus("waitSendOutGoods"); // 待发货——未系统发货前出库单
		List<ShiporderVO> shipOrderList = shipOrderService.getShiporderList(shiporderVO);
		if (shipOrderList.size()> 0) {
			returnValue = false;
		}

		// 2. 校验是否有不符合的补发单
		if (returnValue) {
			ResendVO param = new ResendVO();
			param.setSourceId(sysRefundVO.getOrderId());
			param.setSourceType("or");
			param.setCancelStatus(ResendCancelStatus.N);
			PageInfo<ResendVO> resendList = afterSaleResendService.findByWhere(0, Integer.MAX_VALUE, param);

			if (resendList.getList().size() > 0) {
				for (ResendVO afterSaleResendVO : resendList.getList()) {
					if (ResendStatus.DELIVER.equalsIgnoreCase(afterSaleResendVO.getResendStatus())
							|| ResendStatus.VERIFY.equalsIgnoreCase(afterSaleResendVO.getResendStatus())) {
						returnValue = false;
						break;
					}
				}
			}
		}
		return returnValue;
	}

	/**
	 * @see cn.rkylin.oms.base.refundReasonType.utils.apprhandler.ApprHandler#getRuleCheckArray()
	 */
	private int[] ruleCheckArray = { 2, 3 };

	@Override
	protected int[] getRuleCheckArray() {
		return ruleCheckArray;
	}

}
